package com.ews.onlineexchange.service.test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import javax.enterprise.event.Event;
import javax.enterprise.inject.Model;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ChooseType;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.service.qualifier.MessagePushWsEvent;
import com.ews.onlineexchange.service.qualifier.StartChooseWsEvent;

@Model
@ClientEndpoint
@Transactional
public class BulletinEndClientEndPointService {
	public void sendMessage(Bulletin bulletin) throws DeploymentException, IOException, URISyntaxException {
//		WebSocketContainer container = ContainerProvider.getWebSocketContainer();
//		String uri = "ws://localhost:" + System.getProperty("webServicePort") + "/bulletinEnd/".concat(bulletin.getId());
//		Session session = container.connectToServer(BulletinEndClientEndPointService.class, new URI(uri));
//		if (bulletin.getWayOfChoose().equals(ChooseType.Random) || bulletin.getWayOfChoose().equals(ChooseType.Manual))
//			session.getBasicRemote().sendText(bulletin.getSuccessTrade().getSupplier().getId());
//		else if (bulletin.getWayOfChoose().equals(ChooseType.Best)) {
//			String seeds = new String();
//			for (Trade trade : bulletin.getBestTrades()) {
//				seeds = seeds.concat(trade.getSupplier().getId()).concat(",");
//			}
//			if (seeds.length() > 0)
//				seeds = seeds.substring(0, seeds.length() - 1);
//			session.getBasicRemote().sendText(seeds);
//		}
//		CloseReason closeReason = new CloseReason(CloseCodes.NORMAL_CLOSURE, "Event sended");
//		session.close(closeReason);
	}
	
	@OnMessage
	public void onMessage(String message) {}
	
	@Inject
	EntityManager entityManager;	
	public void sendTestMessage(String bulletinId) throws DeploymentException, IOException, URISyntaxException {
		Bulletin bulletin = findBulletin(bulletinId);
		bulletin.getBestTrades().size();
		sendMessage(bulletin);
	}
	
	private static final String QUERY_BULLETIN_EQL = ""
			+ "	SELECT		b "
			+ "	FROM		Bulletin b"
			+ "	LEFT JOIN FETCH b.bestTrades "
			+ "	WHERE		b.id = :bulletinId	";
	private Bulletin findBulletin(String bulletinId) {
		Query query = entityManager.createQuery(QUERY_BULLETIN_EQL);
		query.setParameter("bulletinId", bulletinId);
		return (Bulletin) query.getResultList().get(0);
	}
	
	@Inject
	@MessagePushWsEvent
	Event<String> messagePushWsEvent;
	private String message;
	public void pushTestMessage() throws DeploymentException, IOException, URISyntaxException {
//		message = message;
		messagePushWsEvent.fire(message);
//        System.out.println("sdasdas");
//		pushMessage("312321312");
	}
	
	public void pushMessage() throws DeploymentException, IOException, URISyntaxException {
		
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
